package com.simon.system.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.simon.http.model.PResponse;
import com.simon.system.dao.DictMapper;
import com.simon.system.model.Dept;
import com.simon.system.model.Dict;
import com.simon.system.service.DictService;
import com.simon.utils.Utils;

@Service
public class DictServiceImpl implements DictService{
	
	@Autowired
	private DictMapper dictMapper;
	
	@Override
	public int addDict(Dict dict) {
		return dictMapper.insert(dict);
	}

	@Override
	public Dict getDictById(int id) {
		return dictMapper.selectByPrimaryKey(id);
	}

	@Override
	public List<Dict> queryDicts(String code, String name, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		List<Dict> dicts = dictMapper.queryDicts(code, name);
		return dicts;
	}

	@Override
	public PResponse calPage(String code, String name, int pageSize) {
		List<Dict> dicts = dictMapper.queryDicts(code, name);
		PResponse bPage = new PResponse();
		if (!dicts.isEmpty()) {
			bPage.setTotalElements(dicts.size());
			bPage.setTotalPage(Utils.calPages(dicts.size(),pageSize));
		}else {
			bPage.setTotalElements(0);
			bPage.setTotalPage(0);
		}
		
		return bPage;
	}

	@Override
	public int deleteDict(int id) {
		return dictMapper.deleteByPrimaryKey(id);
	}

	@Override
	public int updateDict(Dict oldDict, Dict newDict) {
		newDict.setId(oldDict.getId());
		return dictMapper.updateByPrimaryKey(newDict);
	}

	@Override
	public List<Dict> subDictList(String code) {
		return dictMapper.subDictList(code);
	}

	@Override
	public Dict findByCode(String code,int pid) {
		return dictMapper.findByCode(code,pid);
	}

}
